<?php
require_once('DataAccess.php');
require_once('CompraCampus.php');

/**
Interface DAO de Empenho. Estes mï¿½todos Nï¿½O devem ser chamados diretamente.
Ao invï¿½s disso devem ser chamados por mï¿½todos da classe Programa.
*/

class CompraCampusDAO
{ 
      private $database;

      public function __construct( )
      {
               $this->database = DataAccess::getConnection();
      }
      
      public function  CadastrarCompraCampus( $compraCampus )
      {


      	$consulta = $this->database->Fetch("BEGIN");

      	if($consulta)
      	{
      		 
      		$campus = $compraCampus->campus;
      		$compra = $compraCampus->compra;
      		$consulta = $this->database->Fetch("INSERT into t_compra_campus (t_compra_id, t_campus_id, status, valor_total )
      											VALUES($compra->id, $campus->id, $compraCampus->status, $compraCampus->valorTotal)");
      		 
      		 
      		//	$id = DataAccess::getId();
      		//				if($compraCampus->materialCompraCampus != NULL){
      		//					for( $i = 0; $i < count ( $compraCampus->materialCompraCampus); $i++ )
      		//					{
      		//
      		//						try{
      		//							$c = new CompraCampus();
      		//							$compraCampus->materialCompraCampus[$i]->compraCampusId = $c->CarregarCompraCampus($id);
      		//							$compraCampus->materialCompraCampus[$i]->Salvar();
      		//
      		//						}catch(Exception $e)
      		//						{
      		//							$this->database->Fetch("ROLLBACK");
      		//							throw new Exception("Problema na insercao da(s) nota(s) de empenho no banco de dados. Erro:".mysql_error()."");
      		//						}
      		//
      		//					}
      		//				}

      		if( $consulta )
      		{
      			$id = DataAccess::getId();
            	$consulta = $this->database->Fetch("COMMIT");
            	return $id;
      		}
      		else
      		{
      			$consulta = $this->database->Fetch("ROLLBACK");
      			return FALSE;
      		}

      }else{
    		$consulta = $this->database->Fetch("ROLLBACK");
            return FALSE;
    	}	
	}
		
      
      public function Alterar( $compraCampus )
      {
      	$consulta = $this->database->Fetch("BEGIN");

      	
      	if( $consulta )
      	{
      		$compra = $compraCampus->compra;
      		$campus = $compraCampus->campus;
      		$consulta = $this->database->Fetch("UPDATE t_compra_campus 
      											SET t_compra_id = $compra->id ,
      											t_campus_id = $campus->id,
      											status = $compraCampus->status,
      											valor_total = $compraCampus->valorTotal      											
      											where id = $compraCampus->id");
      		if ( $consulta )
      		{
      			$this->database->Fetch("COMMIT");
      			return TRUE;
      		}
      		else
      		{
      			$this->database->Fetch("ROLLBACK");
      			return FALSE;
      		}
      	}
      	else
      	{
      		$this->database->Fetch("ROLLBACK");
      		return FALSE;
      	}
      }
      
      public function ExcluirPorProcesso( $processoId )
      {
      	$consulta = $this->database->Fetch("BEGIN");
      	
      	
      	
      	if( $consulta )
      	{
      		$compraCampusListDel = $this->CarregarPorProcesso($processoId);
      		$materialCompraCampusDao = new MaterialCompraCampus();
      		
      		if($compraCampusListDel != NULL){
      			foreach($compraCampusListDel as $compraCampus){
      				$result = $materialCompraCampusDao->ExcluirPorCompraCampus($compraCampus->id);
      				if(!$result)
      				return FALSE;
      				 
      				$consulta = $this->database->Fetch("Delete from t_compra_campus where id = $compraCampus->id");

      				if ( $consulta )
      				{
      					$this->database->Fetch("COMMIT");

      				}
      				else
      				{
      					$this->database->Fetch("ROLLBACK");
      					return FALSE;
      				}
      			}
      		}
      		return TRUE;
      	}
      	else
      	{
      		$this->database->Fetch("ROLLBACK");
      		return FALSE;
      	}
      	
      }
      
      
      public function LoadCompraCampus( $id )
      {
             $consulta = $this->database->Fetch(" SELECT * FROM t_compra_campus WHERE id = '$id'");
             if( $consulta != FALSE  and mysql_num_rows( $consulta ) > 0)
             {
                  $rows = $this->database->getRow();
                  $objetos = array();

                  for( $i = 0; $i < count($rows); $i++ )
                  {
                       $compraCampus = new CompraCampus();
                       $compraCampus->id = $rows[$i]['id'];
                       $campus = new Campus();                       
                       $compraCampus->campus = $campus->CarregarCampus($rows[$i]['t_campus_id']);
                       $compra = new Processo();
                       $compraCampus = $compra->CarregarProcesso($rows[$i]['t_compra_id']);
                       //$compraCampus->qtd = $rows[$i]['qtd'];
                       $compraCampus->status = $rows[$i]['status'];
                       $compraCampus->valorTotal = $rows[0]['valor_total'];
                       
                       $consulta = $this->database->Fetch(" SELECT * FROM t_material_t_compra_campus WHERE t_compra_campus_id = '$compraCampus->id'");
                       
                       if( $consulta != FALSE )
             			{
                  			$linhas = $this->database->getRow();
                  			$arrayMaterialCompraCampus = array();
                  			for( $j = 0; $j < count($linhas); $j++ )
                  			{    
                       			$arrayMaterialCompraCampus[$j] = new MaterialCompraCampus();
                       			$arrayMaterialCompraCampus[$j]->id = $linhas[$j]['id'];
                       			$arrayMaterialCompraCampus[$j]->compraCampusId = $linhas[$j]['t_compra_campus_id'];
                       			$m = new Material();
                       			$arrayMaterialCompraCampus[$j]->material = $m->CarregarMaterialPorId($linhas[$j]['t_material_id']);
                       			$arrayMaterialCompraCampus[$j]->qtd = $linhas[$j]['qtd'];
                       			$arrayMaterialCompraCampus[$j]->valorTotal = $linhas[$j]['valor_total'];
                       			
                  			}
                  			$compraCampus->materialCompraCampus = $arrayMaterialCompraCampus;
             			}

                       $objetos[] = $compraCampus;
                  }

                  return $objetos;
             }
             else if( mysql_num_rows( $consulta ) == 0 )
             {
                 return NULL;
             }
      }
      
      public function CarregarPorProcessoECampus($idProcesso, $idCampus){
        	$consulta = $this->database->Fetch(" SELECT * FROM t_compra_campus WHERE t_compra_id = $idProcesso AND t_campus_id = $idCampus");
             if( $consulta != FALSE  and mysql_num_rows( $consulta ) > 0)
             {
                  $rows = $this->database->getRow();
                  $objetos = array();

                //  for( $i = 0; $i < count($rows); $i++ )
               //   {
                       $compraCampus = new CompraCampus();
                       $compraCampus->id = $rows[0]['id'];
                       $campus = new Campus();                       
                       $compraCampus->campus = $campus->CarregarCampus($rows[0]['t_campus_id']);
                       $compra = new Processo();
                       $compraCampus->compra = $compra->CarregarProcesso($rows[0]['t_compra_id']);
                      // $compraCampus->qtd = $rows[0]['qtd'];
                       $compraCampus->status = $rows[0]['status'];
                       $compraCampus->valorTotal = $rows[0]['valor_total'];
                       
                       
                       $consulta = $this->database->Fetch(" SELECT * FROM t_material_t_compra_campus WHERE t_compra_campus_id = '$compraCampus->id'");
                       
                       if( $consulta != FALSE and mysql_num_rows( $consulta ) > 0)
             			{
                  			$linhas = $this->database->getRow();
                  			$arrayMaterialCompraCampus = array();
                  			for( $j = 0; $j < count($linhas); $j++ )
                  			{    
                       			$arrayMaterialCompraCampus[$j] = new MaterialCompraCampus();
                       			//$arrayMaterialCompraCampus[$j]->id = $linhas[$j]['id'];
                       			$arrayMaterialCompraCampus[$j]->compraCampusId = $linhas[$j]['t_compra_campus_id'];
                       			$m = new Material();
                       			$arrayMaterialCompraCampus[$j]->material = $m->CarregarMaterialPorId($linhas[$j]['t_material_id']);
                       			$arrayMaterialCompraCampus[$j]->qtd = $linhas[$j]['qtd'];
                       			$arrayMaterialCompraCampus[$j]->valorTotal = $linhas[$j]['valor_total'];
                       			
                  			}
                  			$compraCampus->materialCompraCampus = $arrayMaterialCompraCampus;
             			}

                       $objetos = $compraCampus;
                  //}

                  return $objetos;
          }
             else if( mysql_num_rows( $consulta ) == 0  and mysql_num_rows( $consulta ) > 0)
             {
                 return NULL;
             }
      }
      
      public function CarregarPorProcesso($idProcesso){
      $consulta = $this->database->Fetch(" SELECT * FROM t_compra_campus WHERE t_compra_id = $idProcesso");
             if( $consulta != FALSE  and mysql_num_rows( $consulta ) > 0)
             {
                  $rows = $this->database->getRow();
                  $objetos = array();

                for( $i = 0; $i < count($rows); $i++ )
                 {
                       $compraCampus = new CompraCampus();
                       $compraCampus->id = $rows[$i]['id'];
                       $campus = new Campus();                       
                       $compraCampus->campus = $campus->CarregarCampus($rows[$i]['t_campus_id']);
                       $compra = new Processo();
                       $compraCampus->compra = $compra->CarregarProcesso($rows[$i]['t_compra_id']);
                      // $compraCampus->qtd = $rows[0]['qtd'];
                       $compraCampus->status = $rows[$i]['status'];
                       $compraCampus->valorTotal = $rows[$i]['valor_total'];
                       
                       
                       $consulta = $this->database->Fetch(" SELECT * FROM t_material_t_compra_campus WHERE t_compra_campus_id = '$compraCampus->id'");
                       
                       if( $consulta != FALSE and mysql_num_rows( $consulta ) > 0)
             			{
                  			$linhas = $this->database->getRow();
                  			$arrayMaterialCompraCampus = array();
                  			for( $j = 0; $j < count($linhas); $j++ )
                  			{    
                       			$arrayMaterialCompraCampus[$j] = new MaterialCompraCampus();
                       			//$arrayMaterialCompraCampus[$j]->id = $linhas[$j]['id'];
                       			$compraCampusDao = new CompraCampus();
                       			$arrayMaterialCompraCampus[$j]->compraCampus = $compraCampus;
                       			$m = new Material();
                       			$arrayMaterialCompraCampus[$j]->material = $m->CarregarMaterialPorId($linhas[$j]['t_material_id']);
                       			$arrayMaterialCompraCampus[$j]->qtd = $linhas[$j]['qtd'];
                       			$arrayMaterialCompraCampus[$j]->valorTotal = $linhas[$j]['valor_total'];
                       			
                  			}
                  			$compraCampus->materialCompraCampus = $arrayMaterialCompraCampus;
             			}

                       $objetos[] = $compraCampus;
                  }

                  return $objetos;
          }
             else if( mysql_num_rows( $consulta ) == 0  and mysql_num_rows( $consulta ) > 0)
             {
                 return NULL;
             }
      }
      
}
?>
